package com.baiwei.vpnapi.handler;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.support.RequestContextUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Locale;

/**
 * @program: vpnapi
 * @description: 国际化处理类
 * @author: Mr. Stone
 * @create: 2020-03-05 21:37
 **/

@Component
public class MessageSourceHandler {
    @Autowired
    private HttpServletRequest request;

    @Autowired
    private MessageSource messageSource;

    public String getMessage(String messageKey) {
        // 0 简体中文 1 英文 2 繁体中文
        Integer lang = new Integer(request.getHeader("language"));
        Locale loc = lang == 0 ? Locale.SIMPLIFIED_CHINESE : lang == 1 ? Locale.US : Locale.TAIWAN;
        if(lang == null) {
            loc = Locale.SIMPLIFIED_CHINESE;
        }
        String message = messageSource.getMessage(messageKey, null, loc);
        return message;
    }
}
